package com.xuecheng.media.api;

import com.xuecheng.base.model.RestResponse;
import com.xuecheng.media.model.dto.UploadFileParamsDTO;
import com.xuecheng.media.service.MediaFileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

/**
 * @author Mr.M
 * @version 1.0
 * @description 上传视频
 * @date 2023/2/18 10:34
 */

@Api(value = "大文件上传接口", tags = "大文件上传接口")
@RestController
@RequiredArgsConstructor
public class BigFilesController {

  private final MediaFileService mediaFileService;

  @ApiOperation(value = "文件上传前检查文件")
  @PostMapping("/upload/checkfile")
  public RestResponse<Boolean> checkfile(
      @RequestParam("fileMd5") String fileMd5
  ) throws Exception {
    RestResponse<Boolean> booleanRestResponse = mediaFileService.checkFile(fileMd5);
    return booleanRestResponse;
  }


  @ApiOperation(value = "分块文件上传前的检测")
  @PostMapping("/upload/checkchunk")
  public RestResponse<Boolean> checkchunk(@RequestParam("fileMd5") String fileMd5,
                                          @RequestParam("chunk") int chunk) throws Exception {
    RestResponse<Boolean> booleanRestResponse = mediaFileService.checkChunk(fileMd5, chunk);
    return booleanRestResponse;
  }

  @ApiOperation(value = "上传分块文件")
  @PostMapping("/upload/uploadchunk")
  public RestResponse uploadchunk(@RequestParam("file") MultipartFile file,
                                  @RequestParam("fileMd5") String fileMd5,
                                  @RequestParam("chunk") int chunk) throws Exception {

    //创建一个临时文件
    File tempFile = File.createTempFile("minio", ".temp");
    file.transferTo(tempFile);
    //文件路径
    String localFilePath = tempFile.getAbsolutePath();

    RestResponse restResponse = mediaFileService.uploadChunk(fileMd5, chunk, localFilePath);

    return restResponse;
  }

  @ApiOperation(value = "合并文件")
  @PostMapping("/upload/mergechunks")
  public RestResponse mergechunks(@RequestParam("fileMd5") String fileMd5,
                                  @RequestParam("fileName") String fileName,
                                  @RequestParam("chunkTotal") int chunkTotal) throws Exception {
    Long companyId = 1232141425L;
    //文件信息对象
    UploadFileParamsDTO uploadFileParamsDto = new UploadFileParamsDTO();
    uploadFileParamsDto.setFilename(fileName);
    uploadFileParamsDto.setTags("视频文件");
    uploadFileParamsDto.setFileType("001002");
    RestResponse restResponse = mediaFileService.mergechunks(1232141425L, fileMd5, chunkTotal, uploadFileParamsDto);
    return restResponse;

  }


}
